#include "Temperature.hpp"
#include <dds/dds.hpp>
// #include <dds/sub/NoOpDataReaderListener.hpp> // 1. 加这一行
#include <iomanip>
#include <iostream>
#include <thread>

using namespace SensorData;

class TempListener : public dds::sub::NoOpDataReaderListener<Temperature>
{
  public:
    void on_data_available(dds::sub::DataReader<Temperature> &reader) override
    {
        auto samples = reader.take();
        for (const auto &s : samples)
        {
            if (!s.info().valid())
                continue;
            const Temperature &t = s.data();
            std::cout << std::fixed << std::setprecision(1);
            std::cout << "[alarm] sensor-" << t.sensor_id() << "  " << t.value() << " ℃";
            if (t.value() > 30.0)
                std::cout << "  >>> HIGH TEMP ALERT! <<<";
            std::cout << std::endl;
        }
    }
};

int main(int, char **)
{
    dds::domain::DomainParticipant    dp(0);
    dds::topic::Topic<Temperature>    topic(dp, "TemperatureTopic");
    dds::sub::Subscriber              sub(dp);
    TempListener                      listener;
    // 1. 拿缺省 QoS，最简单：
    dds::sub::qos::DataReaderQos qos = sub.default_datareader_qos();

    // 2. 构造
    dds::sub::DataReader<Temperature> dr(sub, topic, qos, &listener, dds::core::status::StatusMask::data_available());
    std::cout << "Temperature monitor ready …" << std::endl;
    while (true)
        std::this_thread::sleep_for(std::chrono::seconds(1));
    return 0;
}